<?php
/** 问题类
 * Created by PhpStorm.
 * User: chindor
 * Date: 2017/6/14
 * Time: 11:44
 */

namespace app\api\modules\v1\knowledge\models;


use app\api\modules\v1\market\models\DataFormat;
use app\api\modules\v1\market\models\Letter_z;
use app\api\modules\v1\market\models\Message;
use app\api\modules\v1\market\models\SessionMessage;
use app\api\modules\v1\market\models\UploadModel;
use app\models\base\Answer;
use app\models\base\Question;
use app\models\base\User;



class Question_z extends Question
{
    /**问题的发布
     * @param $post
     * @return bool
     */
    public static function post($post)
    {
        $model = new self();
        $file = new UploadModel();

        //保存图片
        if(!empty($_FILES['image']['name'])){

            //如果已经上传了图片
            if(!$file -> uploadImg('image')){
                return false;
            }
            $post['question_img'] = implode(',',(array) SessionMessage::getMess('image'));
        }

        $post['last_modify'] = time();
        $post['post_time']   = time();
        $post['questioner_id'] = $post['user_id'];
        $model -> load($post,'');
        if(!$model -> save()){
            SessionMessage::setMess(array_values($model->getFirstErrors())[0]);
            return false;
        }

        return true;
    }

    /**问题列表的获取
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public static function quesiton_list()
    {
        $question_data = self::find()->asArray()->where(['disabled'=>0])->orderBy(['post_time'=>SORT_DESC])->all();
        if(empty($question_data)){
            SessionMessage::setMess(Message::EMPTY_DATA);
            return false;
        }

        //格式化数据
        foreach($question_data as $k => $val){
            $question_data[$k]['reply_num'] = Answer::find()->where(['question_id'=>$val['question_id']])->count();
            $question_data[$k]['post_time']  = Letter_z::friendlyDate($val['post_time']);
            $question_data[$k]['questioner'] = User::findOne($val['questioner_id'])['name'];
            $question_data[$k]['question_img']  = self::format_path($val['question_img']);
        }

        return $question_data;
    }

    /**问题的回答
     * @param $post
     */
    public static function answer_question($post)
    {
        $question_model = new Answer();
        $file = new UploadModel();

        if(!empty($_FILES['image']['name'])){
            if(!$file->uploadImg('image')){
                return false;
            }
            $post['answer_img'] = implode(',',(array) SessionMessage::getMess('image'));
        }

        $post['answer_user_id'] = $post['user_id'];
        $post['answer_time'] = time();
        $post['last_modify'] = time();
        $question_model -> load($post,'');
        if(!$question_model->save()){
            SessionMessage::setMess(array_values($question_model->getFirstErrors())[0]);
            return false;
        }
        return true;
    }

    /**问题列表的获取
     * @param null $question_id
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public static function AnswerList($question_id=null)
    {
        $answer_data = Answer::find()->asArray()->where(['disabled'=>0])->orderBy(['answer_time'=>SORT_DESC])->filterWhere(['question_id'=>$question_id])->all();
        if(empty($answer_data)){
            SessionMessage::setMess(Message::EMPTY_DATA);
            return false;
        }

        //格式化数据
        foreach($answer_data as $k => $val){
            $answer_data[$k]['answer_time'] = Letter_z::friendlyDate($val['answer_time']);
            $answer_data[$k]['answer']  = User::findOne($val['answer_user_id'])['name'];
            $answer_data[$k]['answer_img'] = self::format_path($val['answer_img']);
        }

        return $answer_data;
    }

    /**格式化路径
     * @param $file_arr
     * @return array
     */
    public static function format_path($file_arr)
    {
        $file = [];
        $data = explode(',',$file_arr);
        foreach($data as $val){
            $file[] = DataFormat::formt_file_path($val);
        }
        return $file;
    }
}